package com.gin.kotlin.no_gay_alliance.fragment.main.me

import android.view.View
import android.widget.CompoundButton
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.adapter.me.CheckInStatusAdapter
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingFragment
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.callback.RCallback
import com.gin.kotlin.no_gay_alliance.databinding.FragmentMainMeCheckInStatusBinding
import com.gin.kotlin.no_gay_alliance.model.CheckInStatusModel
import com.gin.kotlin.no_gay_alliance.provider.ngaphp.CheckInStatusProvider
import com.gin.nga.callback.NgaCallback
import com.gin.nga.response.base.NgaResponse
import com.gin.nga.response.checkin.CheckInResult
import retrofit2.Call
import retrofit2.Response

class CheckInStatusFragment : AbstractBindingFragment<FragmentMainMeCheckInStatusBinding>(), RCallback<CheckInStatusModel, Nothing>, CompoundButton.OnCheckedChangeListener {
    private lateinit var adapter: CheckInStatusAdapter
    private var userId = 0L

    override fun onCreateView() {
        val b = this.b ?: return
        this.adapter = CheckInStatusAdapter(requireContext(), handler!!)
        b.recyclerView.adapter = adapter
        b.swipeRefreshLayout.setOnRefreshListener { request(true) }
        // 手动签到按钮
        b.btCheckIn.setOnClickListener {
            b.btCheckIn.isEnabled = false
            App.INSTANCE.ngaApi.accountApi.checkInPost().enqueue(object : NgaCallback<CheckInResult> {
                override fun onSuccess(
                    call: Call<NgaResponse<CheckInResult>>,
                    response: Response<NgaResponse<CheckInResult>>,
                    body: NgaResponse<CheckInResult>?,
                    data: CheckInResult?
                ) {
                    if (data != null) adapter.replaceData(CheckInStatusModel(data.status!!, data.message).toPairList())
                    // 刷新
                    request(true)
                }

                override fun onError(
                    call: Call<NgaResponse<CheckInResult>>,
                    response: Response<NgaResponse<CheckInResult>>,
                    body: NgaResponse<CheckInResult>?,
                    errorMessage: String
                ) {
                    b.btCheckIn.isEnabled = true
                    toastLong(errorMessage)
                }
            })
        }
        // 自动签到开关
        b.swAutoCheckIn.isChecked = App.INSTANCE.account.autoCheckIn
        // 切换时更新
        b.swAutoCheckIn.setOnCheckedChangeListener(this)

    }

    override fun onResume() {
        super.onResume()
        if (userId != App.INSTANCE.account.userId) {
            request()
            val b = this.b ?: return
            b.swAutoCheckIn.setOnCheckedChangeListener(null)
            b.swAutoCheckIn.isChecked = App.INSTANCE.account.autoCheckIn
            b.swAutoCheckIn.setOnCheckedChangeListener(this)
        }

    }

    private fun request(ignoreCache: Boolean = false) {
        val b = this.b ?: return
        b.tvWarning.visibility = View.GONE
        b.swipeRefreshLayout.isRefreshing = true
        CheckInStatusProvider.getInstance().enqueue(ignoreCache = ignoreCache, callback = this)
    }

    override fun onSuccess(data: CheckInStatusModel, ignoreCache: Boolean) {
        val b = this.b ?: return
        b.swipeRefreshLayout.isRefreshing = false
        this.userId = data.userId ?: 0L
        b.tvMessage.text = data.message

        adapter.replaceData(data.toPairList())
        // 签到按钮
        b.swAutoCheckIn.visibility = View.VISIBLE
        b.btCheckIn.visibility = View.VISIBLE
        b.btCheckIn.isEnabled = data.nowDay != data.lastDay
        b.btCheckIn.setText(if (data.nowDay != data.lastDay) R.string.bt_check_in else R.string.bt_checked_in)
    }

    override fun onError(errorMessage: String) {
        val b = this.b ?: return
        adapter.clear()
        b.swipeRefreshLayout.isRefreshing = false
        b.tvWarning.visibility = View.VISIBLE
        b.swAutoCheckIn.visibility = View.GONE
        b.btCheckIn.visibility = View.GONE
        b.tvWarning.text = errorMessage
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        App.INSTANCE.account.autoCheckIn = isChecked
        App.INSTANCE.database.accountDao().update(App.INSTANCE.account)
        if (isChecked) AlertDialogUtils.builder(requireContext(), message = getString(R.string.dialog_message_auto_check_in)).show()
    }
}